package adb;

import static adb.ADBView.screenWidth;
import static adb.OSK.adbOSK;
import static adb.OSK.commandOSK;
import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.RawImage;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingWorker;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class ScreenShotRealtimePortrait50 extends SwingWorker<Void, String>{

    public static int rawImageWidth;
    public static int rawImageHeight;
    public static int SSRealTimeP50open=0;
    public static AndroidDebugBridge bridge = AndroidDebugBridge.createBridge();
    IDevice device0;
    int firstX=0;
    int firstY=0;
    int mouseClick=0;
    
    @Override
    protected Void doInBackground() throws Exception {
        AndroidDebugBridge.init(false /* debugger support */);
        int count = 0;
        while (bridge.hasInitialDeviceList() == false) {
            try {
                Thread.sleep(100);
                count++;
            } catch (InterruptedException ex) {}
        }
        creationFrameScreenShot();
        return null;
        
    }
    public void creationFrameScreenShot(){
        
        final JFrame framess = new JFrame();
        final JLabel imageLabel50 = new JLabel();

        framess.addWindowListener(new WindowListener() {    
            public void windowOpened(WindowEvent e) {
                SSRealTimeP50open=1;
            }
            public void windowClosing(WindowEvent e) {
                SSRealTimeP50open=0;
            }
            public void windowClosed(WindowEvent e) {
                SSRealTimeP50open=0;
//                bridge=null;
                rawImageWidth=DISPOSE_ON_CLOSE;
                rawImageHeight=DISPOSE_ON_CLOSE;
//                device0=null;
                firstX=DISPOSE_ON_CLOSE;
                firstY=DISPOSE_ON_CLOSE;
                mouseClick=DISPOSE_ON_CLOSE;
                
                framess.removeAll();
                System.gc();
                framess.dispose();
                
            }
            public void windowIconified(WindowEvent e) {
            }
            public void windowDeiconified(WindowEvent e) {
            }
            public void windowActivated(WindowEvent e) {
            }
            public void windowDeactivated(WindowEvent e) {               
            }
        });        
        
        imageLabel50.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                
                firstX = e.getX()*2;
                firstY = e.getY()*2;
            }
        });
        imageLabel50.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                runCommandOnly r = new runCommandOnly();
                commandOSK = adbOSK+" shell input tap "+e.getX()*2+" "+e.getY()*2;
                r.execute();
            }
        });
        imageLabel50.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                int secondX = e.getX()*2;
                int secondY = e.getY()*2;
                if(Math.abs(secondX-firstX)>5||Math.abs(secondY-firstY)>5){
                runCommandOnly r = new runCommandOnly();
                commandOSK = adbOSK+" shell input swipe "+firstX+" "+firstY+" "+secondX+" "+secondY;
                r.execute();
                }
            }
        });
        imageLabel50.setIcon(new ImageIcon(getScreenShot()));
        framess.setSize(rawImageWidth/2+10, rawImageHeight/2+35);
        framess.setLocation((screenWidth - (rawImageWidth/2+10)) / 2,0);
        framess.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        framess.setResizable(false);
        framess.setTitle("Screen Shot");
        framess.getContentPane().add(imageLabel50);
        framess.setVisible(true);
        while(true){
        framess.add(imageLabel50);
        imageLabel50.setIcon(new ImageIcon(getScreenShot()));
        
        framess.remove(imageLabel50);
        }
    }
    public BufferedImage getScreenShot(){
        IDevice[] devices = bridge.getDevices();
        RawImage rawImage = null;
        device0 = devices[0];
        try {
            rawImage = device0.getScreenshot();
            rawImageHeight = rawImage.height;
            rawImageWidth= rawImage.width;
        } catch (IOException ex) {}
        BufferedImage image = new BufferedImage(rawImageWidth,rawImageHeight,BufferedImage.TYPE_INT_ARGB);
        int index = 0;
        int IndexInc = rawImage.bpp >> 3;
        for (int y = 0; y < rawImage.height; y++) {
            for (int x = 0; x < rawImage.width; x++) {
                int value = rawImage.getARGB(index);
                index += IndexInc;
                image.setRGB(x, y, value);
            }
        }
        
        return zoomImage(image);
    }
    public static BufferedImage zoomImage(BufferedImage imgInput){
        BufferedImage imgOutput = new BufferedImage(rawImageWidth/2 , rawImageHeight/2,BufferedImage.TYPE_INT_ARGB );
        Graphics2D g = imgOutput.createGraphics();
        g.drawImage(imgInput, 0, 0, rawImageWidth/2 , rawImageHeight/2 , null);
        g.dispose();
        return imgOutput;
    }


}
